namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using System;

    public class Priority
    {
        private int _Id;
        private string _ImageUrl;
        private string _Name;
        private int _ProjectId;

        public Priority(int projectId, string name) : this(DefaultValues.GetPriorityIdMinValue(), projectId, name, string.Empty)
        {
        }

        public Priority(int projectId, string name, string imageUrl) : this(DefaultValues.GetPriorityIdMinValue(), projectId, name, imageUrl)
        {
        }

        public Priority(int id, int projectId, string name, string imageUrl)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("projectId");
            }
            if ((name == null) || (name.Length == 0))
            {
                throw new ArgumentOutOfRangeException("PriorityName");
            }
            this._Id = id;
            this._ProjectId = projectId;
            this._Name = name;
            this._ImageUrl = imageUrl;
        }

        public static Priority CreateNewPriority(int projectId, string priorityName)
        {
            return CreateNewPriority(projectId, priorityName, string.Empty);
        }

        public static Priority CreateNewPriority(int projectId, string priorityName, string imageUrl)
        {
            Priority newPriority = new Priority(projectId, priorityName, imageUrl);
            if (newPriority.Save())
            {
                return newPriority;
            }
            return null;
        }

        public bool Delete()
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().DeletePriority(this.Id);
        }

        public static bool DeletePriority(int priorityId)
        {
            if (priorityId <= DefaultValues.GetPriorityIdMinValue())
            {
                throw new ArgumentOutOfRangeException("priorityId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().DeletePriority(priorityId);
        }

        public static PriorityCollection GetPrioritiesByProjectId(int projectId)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("priorityId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetPrioritiesByProjectId(projectId);
        }

        public bool Save()
        {
            if (this.Id > DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("priorityId");
            }
            int TempId = DataAccessLayerBaseClassHelper.GetDataAccessLayer().CreateNewPriority(this);
            if (TempId > 0)
            {
                this._Id = TempId;
                return true;
            }
            return false;
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
        }

        public string ImageUrl
        {
            get
            {
                return this._ImageUrl;
            }
        }

        public string Name
        {
            get
            {
                if ((this._Name == null) || (this._Name.Length == 0))
                {
                    return string.Empty;
                }
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }

        public int ProjectId
        {
            get
            {
                return this._ProjectId;
            }
            set
            {
                if (value <= DefaultValues.GetProjectIdMinValue())
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                this._ProjectId = value;
            }
        }
    }
}

